package com.ezandroid.module.network.request;

import com.ezandroid.library.network.http.params.HttpParams;

import java.io.IOException;
import java.util.Map;

/**
 * 封装的多类型post请求,支持上传文件
 *
 * @author markmjw
 * @date 2014-12-26
 */
public abstract class HttpMultipartRequest<T> extends HttpGsonRequest<T> {

    public HttpMultipartRequest(String url) {
        this(url, null);
    }

    public HttpMultipartRequest(String url, String nodeName) {
        this(Method.POST, url, nodeName);
    }

    public HttpMultipartRequest(int method, String url, String nodeName) {
        super(method, url, nodeName);
    }

    @Override
    public final String getBodyContentType() {
        HttpParams params = getMultipartParams();
        if (null != params && params.hasMultipartParams()) {
            try {
                return params.getMultipart().getContentType();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return super.getBodyContentType();
    }

    @Override
    protected final Map<String, String> getParams() {
        HttpParams params = getMultipartParams();
        if (null != params) {
            return params.getTextParams();
        }
        return super.getParams();
    }

    @Override
    public final byte[] getBody() {
        HttpParams params = getMultipartParams();
        if (null != params) {
            try {
                if (params.hasMultipartParams()) {
                    return params.getMultipart().getContentByte();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return super.getBody();
    }

    public abstract HttpParams getMultipartParams();
}
